package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterModelBean;
import com.koron.css2.baseConfig.mapper.MeterModelMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

/**
 * 水表型号-添加
 *
 * @author 甄玉晴
 */
public class MeterModelAdd implements ServerInterface {

    private static Logger logger = LogManager.getLogger(MeterModelAdd.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            MeterModelMapper mapper = factory.getMapper(MeterModelMapper.class);
            MeterModelBean bean = JsonUtils.objectToPojo(req.getData(), MeterModelBean.class);
            // 校验字段重复
            if (mapper.check("model_name", bean.getModelName()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "水表型号名称：" + bean.getModelName() + "的信息已存在。", void.class);
            }
            // 校验字段重复
            if (mapper.check("model_no", bean.getModelNo()) > 0) {
                return MessageBean.create(Constant.ILLEGAL_PARAMETER, "水表型号编号：" + bean.getModelNo() + "的信息已存在。", void.class);
            }
            bean.setCreateInfo(userInfo);
            mapper.insertMeterModel(bean);
        } catch (Exception e) {
            logger.error("水表型号添加失败", e);
            return MessageBean.create(Constant.ILLEGAL_PARAMETER, "水表型号添加失败", void.class);
        }
        return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", void.class);
    }
}